package team.seekers.oj.homework.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import team.seekers.oj.common.annotation.UserSession;
import team.seekers.oj.common.entity.ApiResponseBody;
import team.seekers.oj.common.entity.PageResult;
import team.seekers.oj.common.entity.UserSessionDTO;
import team.seekers.oj.common.enums.ApiExceptionEnum;
import team.seekers.oj.common.exception.InternalApiException;
import team.seekers.oj.common.util.AssertUtils;
import team.seekers.oj.homework.dto.*;
import team.seekers.oj.homework.service.HomeworkManageService;
import team.seekers.oj.homework.service.HomeworkService;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Date;


@Slf4j
@Controller
@RequestMapping("/manage/homework")
public class HomeworkManageController {

    @Autowired
    private HomeworkService homeworkService;

    @Autowired
    private HomeworkManageService homeworkManageService;


    @PostMapping("/create")
    @ApiResponseBody
    public Long create(@RequestBody @Valid HomeworkCreateReqDTO reqDTO,
                       @UserSession UserSessionDTO userSessionDTO) {

        // 增补
        reqDTO.setUserId(userSessionDTO.getUserId());

        // 校验 当前时间<开始时间<结束时间
        AssertUtils.isTrue(reqDTO.getGmtStart().after(new Date()), ApiExceptionEnum.CONTEST_TIME_ERROR);
        AssertUtils.isTrue(reqDTO.getGmtStart().before(reqDTO.getGmtEnd()), ApiExceptionEnum.CONTEST_TIME_ERROR);

        return homeworkManageService.create(reqDTO);
    }

    @GetMapping("/page")
    @ApiResponseBody
    public PageResult<HomeworkListDTO> list(HomeworkListReqDTO reqDTO,
                                            @UserSession(nullable=true) UserSessionDTO userSessionDTO) {
        if(userSessionDTO.getRoles().contains("student")){
            reqDTO.setStudentId(userSessionDTO.getUserId());
        }else if(userSessionDTO.getRoles().contains("teacher")){
            reqDTO.setTeacherId(userSessionDTO.getUserId());
        }
        return homeworkService.page(reqDTO);
    }


    @GetMapping("/query")
    @ApiResponseBody
    public HomeworkManageDTO queryDetail(@RequestParam("contestId") @NotBlank Long homeworkId,
                                         @UserSession UserSessionDTO userSessionDTO) throws InternalApiException {
        AssertUtils.isTrue(homeworkManageService.isHomeworkManager(homeworkId, userSessionDTO),
                ApiExceptionEnum.USER_NOT_MATCHING, "非作业创建人");
        HomeworkManageDTO homeworkManageDTO = homeworkManageService.query(homeworkId);
        return homeworkManageDTO;
    }

    @PostMapping("/update")
    @ApiResponseBody
    public Void update(@RequestBody @Valid HomeworkManageDTO reqDTO,
                       @UserSession UserSessionDTO userSessionDTO) {
        // 校验 开始时间<结束时间
        AssertUtils.isTrue(reqDTO.getGmtStart().before(reqDTO.getGmtEnd()), ApiExceptionEnum.CONTEST_TIME_ERROR);

        AssertUtils.isTrue(homeworkManageService.isHomeworkManager(reqDTO.getId(), userSessionDTO),
                ApiExceptionEnum.USER_NOT_MATCHING, "非作业创建人");

        homeworkManageService.update(reqDTO);
        return null;
    }

}